package com.plian.system.service.glcydgz.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.listener.PageReadListener;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.plian.Tools.CollectionUtil;
import com.plian.Tools.DateUtil;
import com.plian.Tools.StringUtil;
import com.plian.system.bean.pf.business.*;
import com.plian.system.constant.sys.FormTypeConstant;
import com.plian.system.entity.im.ProjectInfo;
import com.plian.system.entity.pf.*;
import com.plian.system.entity.sys.Office;
import com.plian.system.entity.sys.Org;
import com.plian.system.excel.ImportExcelUtil;
import com.plian.system.properties.GzwProperties;
import com.plian.system.service.glc.IGlcBaseBusinessIndicatorsService;
import com.plian.system.service.glcydgz.IGlcBaseBusinessAnnualService;
import com.plian.system.service.glcydgz.IGlcBaseBusinessAuditService;
import com.plian.system.service.glcydgz.IGlcBusinessImportService;
import com.plian.system.service.im.IProjectInfoService;
import com.plian.system.service.pf.*;
import com.plian.system.service.sys.office.IOfficeService;
import com.plian.system.service.sys.orgzation.IOrgService;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * @Description: TODO
 * @Author: Take-off
 * @Date: 2023/6/30 11:09 AM
 * @Param
 * @return
 * @Version: 1.0
 **/
@Service
public class GlcBusinessImportServiceImpl implements IGlcBusinessImportService {

    @Autowired
    private IGlcBaseBusinessIndicatorsService indicatorsService;

    @Autowired
    private IGlcBaseBusinessAnnualService annualService;

    @Autowired
    private IGlcBaseBusinessAuditService auditService;

    @Autowired
    private IOrgService orgService;

    @Autowired
    private IAppraisalIndexService appraisalIndexService;

    @Autowired
    private IProjectInfoService projectInfoService;

    @Autowired
    private IPerformanceScoreService performanceScoreService;

    @Autowired
    private GzwProperties gzwProperties;

    @Autowired
    private IOfficeService officeService;

    @Override
    public void uploadFilePerformance(MultipartFile multipartFile){
        try {
            byte[] bytes = IOUtils.toByteArray(multipartFile.getInputStream());
            InputStream inputStream = new ByteArrayInputStream(bytes);
            //主表信息
            List<Object> performanceBeanList = new ArrayList<>();
            EasyExcel.read(inputStream).build()
                    .read(EasyExcel.readSheet(0).headRowNumber(1).head(PerformanceBean.class)
                            .registerReadListener(new PageReadListener<>(performanceBeanList::addAll)).build());
            ImportExcelUtil.filterEmpty(performanceBeanList);
            BasePerformanceEffect indicators = getIndicators(performanceBeanList);
            performanceScoreService.submit(indicators);
        }catch (IOException | IllegalAccessException e) {
            e.printStackTrace();
        }

    }

    /**
     * @Description: //TODO 履职成效标准转化
     * @Author: Take-off
     * @Date: 4:42 PM 2023/7/4
     * @Param: [performanceBeanList]
     * @return: com.plian.system.entity.pf.BasePerformanceEffect
     **/
    private BasePerformanceEffect getIndicators(List<Object> performanceBeanList){
        BasePerformanceEffect performanceEffect = new BasePerformanceEffect();

        PerformanceBean bean = (PerformanceBean)performanceBeanList.get(0);
        performanceEffect.setFillingYear( bean.getFillingYear());
        performanceEffect.setReportOffice(Long.valueOf(gzwProperties.getAssessmentId()));
        List<PerformanceScoreMethod> scoreMethods = new ArrayList<>();
        List<Office> offices = officeService.list();
        //code
        List<AppraisalIndex> indexList = appraisalIndexService.listByTableCode("003");
        for (Object object:performanceBeanList){
            PerformanceBean performanceBean = (PerformanceBean)object;
            PerformanceScoreMethod scoreMethod = new PerformanceScoreMethod();
            scoreMethod.setIndexNum(performanceBean.getIndexNum());
            scoreMethod.setCheckItem(performanceBean.getCheckItem());
            scoreMethod.setWeight(performanceBean.getWeight());
            scoreMethod.setScoreMethod(performanceBean.getScoreMethod());
            scoreMethod.setIsLast(1);
            //指标获取
            indexList.stream().filter(m-> performanceBean.getIndicatorsName().equals(m.getName())).findFirst().ifPresent(index ->{
                scoreMethod.setBaseBusinessIndicatorsId(index.getId());
            });
            //责任处室
            offices.stream().filter(m -> Optional.ofNullable(performanceBean.getReportOfficeName()).isPresent() && performanceBean.getReportOfficeName().equals(m.getName())).findFirst().ifPresent(office ->{
                scoreMethod.setReportOffice(office.getId());
            });
            scoreMethods.add(scoreMethod);
        }
        performanceEffect.setPerformanceScoreMethods(scoreMethods);
        return performanceEffect;
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void uploadFile(MultipartFile multipartFile){
        try {
            byte[] bytes = IOUtils.toByteArray(multipartFile.getInputStream());
            InputStream inputStream = new ByteArrayInputStream(bytes);
            //主表信息
            List<Object> baseList = new ArrayList<>();
            //财务绩效
            List<Object> businessList = new ArrayList<>();
            //任务绩效
            List<Object> taskList = new ArrayList<>();
            //重大工程考核
            List<Object> projectList = new ArrayList<>();
            //企业自评
            List<Object> assetList = new ArrayList<>();
            //主责主业
            List<Object> auditList = new ArrayList<>();
            EasyExcel.read(inputStream).build()
                    .read(EasyExcel.readSheet(1).headRowNumber(2).head(BusinessIndicatorBean.class)
                            .registerReadListener(new PageReadListener<>(baseList::addAll)).build());
            inputStream.reset();
            ImportExcelUtil.filterEmpty(baseList);
            EasyExcel.read(inputStream).build()
                    .read(EasyExcel.readSheet(2).headRowNumber(2).head(BusinessExcelBean.class)
                            .registerReadListener(new PageReadListener<>(businessList::addAll)).build());
            inputStream.reset();
            ImportExcelUtil.filterEmpty(businessList);
            EasyExcel.read(inputStream).build()
                    .read(EasyExcel.readSheet(3).headRowNumber(2).head(TaskExcelBean.class)
                            .registerReadListener(new PageReadListener<>(taskList::addAll)).build());
            inputStream.reset();
            ImportExcelUtil.filterEmpty(taskList);
            EasyExcel.read(inputStream).build()
                    .read(EasyExcel.readSheet(4).headRowNumber(2).head(MajorProjectBean.class)
                            .registerReadListener(new PageReadListener<>(projectList::addAll)).build());
            inputStream.reset();
            ImportExcelUtil.filterEmpty(projectList);
            EasyExcel.read(inputStream).build()
                    .read(EasyExcel.readSheet(5).headRowNumber(2).head(AssetsExcelBean.class)
                            .registerReadListener(new PageReadListener<>(assetList::addAll)).build());
            inputStream.reset();
            ImportExcelUtil.filterEmpty(assetList);
            EasyExcel.read(inputStream).build()
                    .read(EasyExcel.readSheet(6).headRowNumber(2).head(MainAuditBean.class)
                            .registerReadListener(new PageReadListener<>(auditList::addAll)).build());
            inputStream.reset();
            ImportExcelUtil.filterEmpty(auditList);
            //code
            List<AppraisalIndex> indexList = appraisalIndexService.listByTableCode("001");
            List<ProjectInfo> projectInfos = projectInfoService.list();
            //考核主表
            BaseBusinessIndicators indicators = getIndicators(baseList, businessList, taskList, projectList, auditList, indexList, projectInfos);
            if (Optional.ofNullable(indicators).isPresent()){
                boolean flag = indicatorsService.submit(indicators);
                if (flag){
                    //年报
                    BaseBusinessAnnual annual = getAnnual(indicators, businessList, taskList, auditList, assetList);
                    flag = annualService.submit(annual);
                    if (flag){
                        //国资审核
                        BaseBusinessAudit audit = getAudit(annual, businessList, taskList, auditList, assetList);
                        flag = auditService.submit(audit);
                        if (flag){
                            //回调年报
                            annual.setBaseAuditId(audit.getId());
                            annualService.saveOrUpdate(annual);
                        }
                    }
                }
            }
        }catch (IOException | IllegalAccessException e) {
            e.printStackTrace();
        }

    }

    /**
     * @Description:  转换成经营绩效考核主表
     * @Author: Take-off
     * @Date: 4:28 PM 2023/6/30
     * @Param: [baseList, businessList, taskList, projectList, auditList, indexList, projectInfos]
     * @return: com.plian.system.entity.pf.BaseBusinessIndicators
     **/
    private BaseBusinessIndicators getIndicators(List<Object> baseList,List<Object> businessList,List<Object> taskList,
                                                 List<Object> projectList,List<Object> auditList,
                                                 List<AppraisalIndex> indexList,List<ProjectInfo> projectInfos){
        BaseBusinessIndicators baseBusinessIndicators = new BaseBusinessIndicators();
        if (CollectionUtil.isNotEmpty(baseList) && baseList.size() ==1){
            BusinessIndicatorBean excelBean = (BusinessIndicatorBean) baseList.get(0);
            baseBusinessIndicators.setFillingYear(excelBean.getFillingYear());
            baseBusinessIndicators.setTargetValue(excelBean.getTargetValue());
            baseBusinessIndicators.setRemarks(excelBean.getRemarks());
            baseBusinessIndicators.setIsMajorProject(excelBean.getIsMajorProject());
            baseBusinessIndicators.setStatus(3);
            baseBusinessIndicators.setModuleType(2);
            Optional.ofNullable(excelBean.getFillingUnit()).ifPresent(unit ->{
                Org org = orgService.getOne(Wrappers.<Org>lambdaQuery().eq(Org::getName, unit));
                baseBusinessIndicators.setFillingUnitId(org.getId());
                baseBusinessIndicators.setCreateCompanyId(org.getId());
            });
            Date now = DateUtil.now();
            baseBusinessIndicators.setFillingTime(now);
            //财务绩效
            List<BusinessIndicators> businessIndicators = new ArrayList<>();
            if (CollectionUtil.isNotEmpty(businessList)){
                for (Object business:businessList){
                    BusinessIndicators businessIndicator = new BusinessIndicators();
                    BusinessExcelBean businessExcelBean = (BusinessExcelBean) business;
                    businessIndicator.setIndicatorsWeight(businessExcelBean.getIndicatorsWeight());
                    businessIndicator.setBaseline(businessExcelBean.getBaseline());
                    businessIndicator.setTarget(businessExcelBean.getTarget());
                    businessIndicator.setBasicTarget(businessExcelBean.getBasicTarget());
                    businessIndicator.setIndicatorsCategoryName(businessExcelBean.getIndicatorsCategoryName());
                    if (Optional.ofNullable(businessExcelBean.getIndicatorsCode()).isPresent()){
                        if ("KH7".equals(businessExcelBean.getIndicatorsCode())){
                            indexList.stream().filter(m ->m.getType() == 1 && m.getFlag() == 2).findFirst().ifPresent(code ->{
                                businessIndicator.setIndicatorsCategoryId(code.getId());
                            });
                        }else {
                            indexList.stream().filter(m ->businessExcelBean.getIndicatorsCode().equals(m.getCompleteCode())).findFirst().ifPresent(code ->{
                                businessIndicator.setIndicatorsCategoryId(code.getId());
                                businessIndicator.setIndicatorsCategoryName(code.getName());
                                businessExcelBean.setIndicatorsCategoryName(code.getName());
                            });
                        }
                    }
                    businessIndicators.add(businessIndicator);
                }
            }
            baseBusinessIndicators.setBusinessIndicators(businessIndicators);
            //任务绩效
            List<TaskIndicators> taskIndicators = new ArrayList<>();
            if (CollectionUtil.isNotEmpty(taskList)){
                for (Object task:taskList){
                    TaskIndicators taskIndicator = new TaskIndicators();
                    TaskExcelBean taskExcelBean = (TaskExcelBean) task;
                    taskIndicator.setIndexNum(taskExcelBean.getIndexNum());
                    taskIndicator.setContent(taskExcelBean.getContent());
                    if (Optional.ofNullable(taskExcelBean.getIndicatorsCode()).isPresent()){
                        if ("ZDRW".equals(taskExcelBean.getIndicatorsCode())){
                            indexList.stream().filter(m ->m.getType() == 2 && m.getSort() == 1).findFirst().ifPresent(code ->{
                                taskIndicator.setIndicatorsCategoryId(code.getId());
                            });
                        }else if ("LSRW".equals(taskExcelBean.getIndicatorsCode())){
                            indexList.stream().filter(m ->m.getType() == 2 && m.getSort() == 2).findFirst().ifPresent(code ->{
                                taskIndicator.setIndicatorsCategoryId(code.getId());
                            });
                        }
                    }
                    taskIndicators.add(taskIndicator);
                }
            }
            baseBusinessIndicators.setTaskIndicators(taskIndicators);
            //重大工程考核
            List<MajorProject> majorProjects = new ArrayList<>();
            if (CollectionUtil.isNotEmpty(projectList)){
                for (Object project:projectList){
                    MajorProjectBean projectBean = (MajorProjectBean) project;
                    MajorProject majorProject = new MajorProject();
                    majorProject.setIndexNum(projectBean.getIndexNum());
                    majorProject.setProjectName(projectBean.getProjectName());
                    majorProject.setRemarks(projectBean.getRemarks());
                    if (Optional.ofNullable(projectBean.getProjectName()).isPresent()){
                        projectInfos.stream().filter(m -> projectBean.getProjectName().equals(m.getProjectName())).findFirst().ifPresent(proInfo ->{
                            majorProject.setProjectId(proInfo.getId());
                        });
                    }
                    majorProjects.add(majorProject);
                }
            }
            baseBusinessIndicators.setMajorProjects(majorProjects);
            //主责主业
            List<MainBusiness> mainBusinesses=new ArrayList<>();
            if (CollectionUtil.isNotEmpty(auditList)){
                for (Object audit:auditList){
                    MainBusiness mainBusiness = new MainBusiness();
                    MainAuditBean auditBean = (MainAuditBean) audit;
                    mainBusiness.setIndexNum(auditBean.getIndexNum());
                    mainBusiness.setEvaluation(auditBean.getEvaluation());
                    mainBusiness.setRemarks(auditBean.getRemarks());
                    mainBusinesses.add(mainBusiness);
                }
            }
            baseBusinessIndicators.setMainBusinesses(mainBusinesses);
        }else {
            return null;
        }
        return baseBusinessIndicators;
    }

    /**
     * @Description:  转换经营绩效年报
     * @Author: Take-off
     * @Date: 9:47 AM 2023/7/3
     * @Param: [baseIndicator, businessList, taskList, auditList, assetList]
     * @return: com.plian.system.entity.pf.BaseBusinessAnnual
     **/
    private BaseBusinessAnnual getAnnual(BaseBusinessIndicators baseIndicator,List<Object> businessList,List<Object> taskList,
                                                 List<Object> auditList, List<Object> assetList){
        BaseBusinessAnnual annual = new BaseBusinessAnnual();
        annual.setFillingYear(baseIndicator.getFillingYear());
        annual.setFillingTime(baseIndicator.getFillingTime());
        annual.setFillingUnitId(baseIndicator.getFillingUnitId());
        annual.setBaseBusinessIndicatorsId(baseIndicator.getId());
        annual.setIsAudit(1);
        annual.setStatus(3);
        annual.setModuleType(1);
        //绩效
        List<BusinessAnnual> businessAnnuals = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(businessList)) {
            List<BusinessIndicators> businessIndicators = baseIndicator.getBusinessIndicators();
            for (Object business : businessList) {
                BusinessAnnual bus = new BusinessAnnual();
                BusinessExcelBean excelBean = (BusinessExcelBean) business;
                bus.setAnnualSubmit(excelBean.getAnnualSubmit());
                bus.setIndicatorsCodeId(excelBean.getIndicatorsCode());
                Optional<BusinessIndicators> optional = businessIndicators.stream().filter(m -> excelBean.getIndicatorsCategoryName().equals(m.getIndicatorsCategoryName())).findFirst();
                if (optional.isPresent()){
                    BusinessIndicators indicator= optional.get();
                    bus.setBusinessIndicatorsId(indicator.getId());
                    bus.setIndicatorsWeight(indicator.getIndicatorsWeight());
                    bus.setBaseline(indicator.getBaseline());
                    bus.setTarget(indicator.getTarget());
                    bus.setBasicTarget(indicator.getBasicTarget());
                }
                businessAnnuals.add(bus);
            }
        }
        annual.setBusinessAnnuals(businessAnnuals);
        //任务
        List<TaskAnnual> taskAnnuals = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(taskList)){
            for (Object task:taskList){
                TaskAnnual taskAnnual = new TaskAnnual();
                TaskExcelBean taskExcelBean = (TaskExcelBean) task;
                taskAnnual.setExecution(taskExcelBean.getAnnualSummary());
                taskAnnual.setSelfScoring(taskExcelBean.getSelfScoring());
                taskAnnual.setIndicatorsCodeId(taskExcelBean.getIndicatorsCode());
                List<TaskIndicators> taskIndicators = baseIndicator.getTaskIndicators();
                if (CollectionUtil.isNotEmpty(taskIndicators)){
                    Optional<TaskIndicators> optional = taskIndicators.stream().filter(m -> taskExcelBean.getIndexNum().equals(m.getIndexNum()) && taskExcelBean.getContent().equals(m.getContent())).findFirst();
                    if (optional.isPresent()){
                        TaskIndicators indicator= optional.get();
                        taskAnnual.setTaskIndicatorsId(indicator.getId());
                        taskAnnual.setIndexNum(indicator.getIndexNum());
                        taskAnnual.setContent(indicator.getContent());
                    }
                }
                taskAnnuals.add(taskAnnual);
            }
        }
        annual.setTaskAnnuals(taskAnnuals);
        //主责主业
        List<MainAnnual> mainAnnuals = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(auditList)){
            for (Object audit:auditList){
                MainAnnual mainAnnual = new MainAnnual();
                MainAuditBean auditBean = (MainAuditBean) audit;
                mainAnnual.setExecution(auditBean.getExecution());
                mainAnnual.setSelfScore(auditBean.getSelfScore());
                mainAnnual.setIndicatorsCodeId(auditBean.getIndexNum() +"");
                List<MainBusiness> mainBusinesses = baseIndicator.getMainBusinesses();
                if (CollectionUtil.isNotEmpty(mainBusinesses)){
                    Optional<MainBusiness> optional = mainBusinesses.stream().filter(m -> auditBean.getIndexNum().equals(m.getIndexNum())).findFirst();
                    if (optional.isPresent()){
                        MainBusiness mainBusiness = optional.get();
                        mainAnnual.setMainBusinessId(mainBusiness.getId());
                    }
                }
                mainAnnuals.add(mainAnnual);
            }
        }
        annual.setMainAnnuals(mainAnnuals);
        //企业自评
        List<SelfAssessment> selfAssessments = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(assetList)){
            for (Object asset:assetList){
                SelfAssessment assessment = new SelfAssessment();
                AssetsExcelBean assetsBean = (AssetsExcelBean) asset;
                assessment.setExecution(assetsBean.getExecution());
                assessment.setSelfScore(assetsBean.getSelfScore());
                assessment.setType(assetsBean.getType());
                selfAssessments.add(assessment);
            }
        }
        annual.setSelfAssessments(selfAssessments);
        return annual;
    }

    /**
     * @Description:  国资审核
     * @Author: Take-off
     * @Date: 11:03 AM 2023/7/3
     * @Param: [baseAnnual, businessList, taskList, auditList, assetList]
     * @return: com.plian.system.entity.pf.BaseBusinessAudit
     **/
    private BaseBusinessAudit getAudit(BaseBusinessAnnual baseAnnual,List<Object> businessList,List<Object> taskList,
                                       List<Object> auditList, List<Object> assetList){
        BaseBusinessAudit businessAudit = new BaseBusinessAudit();
        Date now = DateUtil.now();
        businessAudit.setFillingYear(baseAnnual.getFillingYear());
        businessAudit.setAuditTime(now);
        businessAudit.setApprovedTime(now);
        businessAudit.setFillingUnitId(baseAnnual.getFillingUnitId());
        businessAudit.setFormType(FormTypeConstant.PF_BUSINESS_AUDIT +"");
        businessAudit.setBaseBusinessIndicatorsId(baseAnnual.getBaseBusinessIndicatorsId());
        businessAudit.setBaseAnnalId(baseAnnual.getId());
        businessAudit.setIssued(0);
        businessAudit.setIsDeleted(0);
        businessAudit.setStatus(3);
        businessAudit.setModuleType(1);
        if(baseAnnual!=null&&baseAnnual.getStaffId()!=null){
            businessAudit.setStaffId(baseAnnual.getStaffId());
        }
        if(baseAnnual!=null&& StringUtil.isNotBlank(baseAnnual.getStaffName())){
            businessAudit.setStaffName(baseAnnual.getStaffName());
        }
        //绩效
        List<BusinessAudit> businessAudits = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(businessList)) {
            List<BusinessAnnual> businessAnnuals = baseAnnual.getBusinessAnnuals();
            for (Object business : businessList) {
                BusinessAudit bus = new BusinessAudit();
                BusinessExcelBean excelBean = (BusinessExcelBean) business;
                bus.setAnnualSubmit(excelBean.getAnnualSubmit());
                bus.setAuditFirmly(excelBean.getAuditFirmly());
                bus.setAuditSum(excelBean.getAuditSum());
                Optional<BusinessAnnual> optional = businessAnnuals.stream().filter(m -> excelBean.getIndicatorsCode().equals(m.getIndicatorsCodeId())).findFirst();
                if (optional.isPresent()){
                    BusinessAnnual businessAnnual= optional.get();
                    bus.setBusinessIndicatorsId(businessAnnual.getBusinessIndicatorsId());
                    bus.setIndicatorsWeight(businessAnnual.getIndicatorsWeight());
                    bus.setBaseline(businessAnnual.getBaseline());
                    bus.setTarget(businessAnnual.getTarget());
                    bus.setBasicTarget(businessAnnual.getBasicTarget());
                }
                businessAudits.add(bus);
            }
        }
        businessAudit.setBusinessAudits(businessAudits);
        //任务
        List<TaskAudit> taskAudits = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(taskList)){
            List<TaskAnnual> taskAnnuals = baseAnnual.getTaskAnnuals();
            for (Object task : taskList) {
                TaskAudit taskAudit = new TaskAudit();
                TaskExcelBean excelBean = (TaskExcelBean) task;
                taskAudit.setAnnualSummary(excelBean.getAnnualSummary());
                taskAudit.setSelfScoring(excelBean.getSelfScoring());
                taskAudit.setAuditScoring(excelBean.getAuditScoring());
                taskAudit.setAuditResult(excelBean.getAuditResult());
                Optional<TaskAnnual> optional = taskAnnuals.stream().filter(m -> excelBean.getIndexNum().equals(m.getIndexNum())&& excelBean.getContent().equals(m.getContent())).findFirst();
                if (optional.isPresent()){
                    TaskAnnual taskAnnual= optional.get();
                    taskAudit.setTaskIndicatorsId(taskAnnual.getTaskIndicatorsId());
                }
                taskAudits.add(taskAudit);
            }
        }
        businessAudit.setTaskAudits(taskAudits);
        //主责主业
        List<MainAudit> mainAudits = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(auditList)){
            List<MainAnnual> mainAnnuals = baseAnnual.getMainAnnuals();
            for (Object audit:auditList){
                MainAudit mainAudit = new MainAudit();
                MainAuditBean auditBean = (MainAuditBean) audit;
                mainAudit.setAuditSituation(auditBean.getAuditSituation());
                mainAudit.setAuditScore(auditBean.getAuditScore());
                Optional<MainAnnual> optional = mainAnnuals.stream().filter(m -> auditBean.getIndexNum().toString().equals(m.getIndicatorsCodeId())).findFirst();
                if (optional.isPresent()){
                    MainAnnual mainAnnual = optional.get();
                    mainAudit.setMainBusinessId(mainAnnual.getMainBusinessId());
                }
                mainAudits.add(mainAudit);
            }
        }
        businessAudit.setMainAudits(mainAudits);
        //企业自评
        List<AuditAssets> auditAssets = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(assetList)){
            List<SelfAssessment> selfAssessments = baseAnnual.getSelfAssessments();
            for (Object asset:assetList){
                AuditAssets auditAsset = new AuditAssets();
                AssetsExcelBean assetsBean = (AssetsExcelBean) asset;
                auditAsset.setExecution(assetsBean.getExecution());
                auditAsset.setSelfScore(assetsBean.getSelfScore());
                auditAsset.setAuditExecution(assetsBean.getAuditExecution());
                auditAsset.setAuditSelfScore(assetsBean.getAuditSelfScore());
                auditAsset.setType(assetsBean.getType());
                Optional<SelfAssessment> optional = selfAssessments.stream().filter(m -> assetsBean.getType().equals(m.getType())).findFirst();
                if (optional.isPresent()){
                    SelfAssessment assessment = optional.get();
                    auditAsset.setBaseAssessId(assessment.getId());
                }
                auditAssets.add(auditAsset);
            }
        }
        businessAudit.setAuditAssets(auditAssets);
        return businessAudit;
    }
}
